﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace 使用LinQ访问XML
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btnReadAll_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(@"..\..\Book.xml");
            IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                             select ele;
            showInfoByElements(elements);
        }

        private void showInfoByElements(IEnumerable<XElement> elements)
        {
            List<BookModel> modelList = new List<BookModel>();
            foreach (var ele in elements)
            {
                BookModel model = new BookModel();
                model.BookAuthor = ele.Element("author").Value;
                model.BookName = ele.Element("title").Value;
                model.BookPrice = Convert.ToDouble(ele.Element("price").Value);
                model.BookISBN=ele.Attribute("ISBN").Value;
                model.BookType=ele.Attribute("Type").Value;
                
                modelList.Add(model);
            }
            dgvBookInfo.DataSource = modelList;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvBookInfo.CurrentRow != null)
            {
                //dgvBookInfo.CurrentRow.Cells[1]对应着ISBN号
                string id = dgvBookInfo.CurrentRow.Cells[1].Value.ToString();
                XElement xe = XElement.Load(@"..\..\Book.xml");
                IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                                 where (string)ele.Attribute("ISBN") == id
                                                 select ele;
                if (elements.Count() > 0)
                {
                    elements.First().Remove();
                }
                xe.Save(@"..\..\Book.xml");
                MessageBox.Show("删除成功！");
                btnReadAll_Click(sender, e);

            }
        }

        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(@"..\..\Book.xml");
            IEnumerable<XElement> elements = from ele in xe.Elements("book")
                                             select ele;
            if (elements.Count() > 0)
            {
                elements.Remove();
            }
            xe.Save(@"..\..\Book.xml");
            MessageBox.Show("删除成功！");
            btnReadAll_Click(sender, e);
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(@"..\..\Book.xml");
            XElement record = new XElement(
            new XElement("book",
            new XAttribute("Type", "选修课"),
            new XAttribute("ISBN","7-111-19149-1"),
            new XElement("title", "计算机操作系统"),
            new XElement("author", "7-111-19149-1"),
            new XElement("price", 28.00)));
            xe.Add(record);
            xe.Save(@"..\..\Book.xml");
            MessageBox.Show("插入成功！");
            btnReadAll_Click(sender, e);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            XElement xe = XElement.Load(@"..\..\Book.xml");
            if (dgvBookInfo.CurrentRow != null)
            {
                //dgvBookInfo.CurrentRow.Cells[1]对应着ISBN号
                string id = dgvBookInfo.CurrentRow.Cells[1].Value.ToString();
                IEnumerable<XElement> element = from ele in xe.Elements("book")
                                                where ele.Attribute("ISBN").Value == id
                                                select ele;
                if (element.Count() > 0)
                {
                    XElement first = element.First();
                    ///设置新的属性
                    first.SetAttributeValue("Type", dgvBookInfo.CurrentRow.Cells[0].Value.ToString());
                    ///替换新的节点
                    first.ReplaceNodes(
                             new XElement("title", dgvBookInfo.CurrentRow.Cells[2].Value.ToString()),  
                             new XElement("author", dgvBookInfo.CurrentRow.Cells[3].Value.ToString()),
                             new XElement("price", (double)dgvBookInfo.CurrentRow.Cells[4].Value) 
                             );
                }
                xe.Save(@"..\..\Book.xml");

                MessageBox.Show("修改成功！");
                btnReadAll_Click(sender, e);
            }
        }

        private void dgvBookInfo_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //显示行号
            try
            {
                Rectangle rectangle = new Rectangle(e.RowBounds.Location.X, e.RowBounds.Location.Y, dgvBookInfo.RowHeadersWidth - 4, e.RowBounds.Height);
                TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(), dgvBookInfo.RowHeadersDefaultCellStyle.Font, rectangle,
                dgvBookInfo.RowHeadersDefaultCellStyle.ForeColor, TextFormatFlags.VerticalCenter | TextFormatFlags.Right);

            }
            catch { }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            this.dgvBookInfo.RowsDefaultCellStyle.BackColor = Color.Bisque;
            this.dgvBookInfo.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
        }
    }
}
